package com.lanmaoly.web.server;

import com.lanmaoly.web.common.exception.ArbitrationException;
import com.lanmaoly.web.common.response.CommonCode;
import com.lanmaoly.web.common.response.ResponseResult;
import cunzheng.sdk.utils.HttpClientSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * base
 *
 * @author hou 2018-12-4 17:45:52
 */
public class BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
    @Autowired
    protected HttpServletRequest request;

    @ModelAttribute
    protected void setUrl(Model model) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        if ("anonymousUser".equals(name)) {
            model.addAttribute("isLogin", false);
        } else {
            model.addAttribute("username", authentication.getName());
            model.addAttribute("isLogin", true);
        }
    }

    /*@InitBinder
    protected void initBinder(WebDataBinder binder) {
        // String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
        binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
            }
            @Override
            public String getAsText() {
                Object value = getValue();
                return value != null ? value.toString() : "";
            }
        });
    }*/

    /**
     * 发送请求
     *
     * @param host       接口地址
     * @param method     接口方法
     * @param sendData   接口报文
     * @param customerNo customerNo
     * @return s
     * @throws IOException e
     */
    protected String sendPost(String host, String method, String sendData, String customerNo) throws IOException {
        Map<String, String> headers = new HashMap<>(1);
        headers.put("customerNo", customerNo);
        return HttpClientSender.sendHttpPost(host, method, sendData, headers);
    }

    protected HttpSession getSession() {
        return request.getSession();
    }

    protected Object getSessionAttribute(String key) {
        return getSession().getAttribute(key);
    }

    protected void setSessionAttribute(String key, Object value) {
        getSession().setAttribute(key, value);
    }

    protected String getKaptcha(String key) {
        Object kaptcha = getSessionAttribute(key);
        logger.info("kaptacha:{}", kaptcha);
        if (kaptcha == null) {
            throw new ArbitrationException(CommonCode.VERIFY_CODE_EXPIRED);
        }
        getSession().removeAttribute(key);
        return kaptcha.toString();
    }

    protected ResponseResult result(CommonCode commonCode) {
        return new ResponseResult(commonCode);
    }

    protected ResponseResult result(CommonCode commonCode, Object data) {
        return new ResponseResult(commonCode, data);
    }
}
